package com.marquisx.tzdice;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageButton;

/**
 * A grid that displays a set of framed photos.
 */
public class ActionPicGrid extends Activity implements OnClickListener
{

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		setContentView(R.layout.action_pic_grid);

		GridView g = (GridView) findViewById(R.id.action_pic_grid);
		g.setAdapter(new ImageAdapter(this));
	}

	@Override
	public void onClick(View v)
	{
		int picId = Integer.parseInt((String)v.getTag());
		
		getSharedPreferences(Constants.PREF, 0)
			.edit()
			.putInt(Constants.SELECTED_PIC_ID, picId)
			.commit();
		
		finish();
	}

	public class ImageAdapter extends BaseAdapter
	{
		public ImageAdapter(Context c)
		{
			mContext = c;
		}

		public int getCount()
		{
			return mThumbIds.length;
		}

		public Object getItem(int position)
		{
			return position;
		}

		public long getItemId(int position)
		{
			return position;
		}

		public View getView(int position, View convertView, ViewGroup parent)
		{
			ImageButton imageBtn;
			if (convertView == null)
			{
				imageBtn = new ImageButton(mContext);
				imageBtn.setLayoutParams(
						new GridView.LayoutParams(
							ViewGroup.LayoutParams.WRAP_CONTENT, 
							ViewGroup.LayoutParams.WRAP_CONTENT)
				);
			}
			else
			{
				imageBtn = (ImageButton) convertView;
			}

			if(getResources().getBoolean(R.bool.app_version_pro))
			{
				imageBtn.setImageResource(mThumbIds_pro[position]);
				imageBtn.setTag(mThumbIds_pro[position] + "");
			}
			else
			{
				imageBtn.setImageResource(mThumbIds[position]);
				imageBtn.setTag(mThumbIds[position] + "");
			}

			imageBtn.setOnClickListener(ActionPicGrid.this);
			return imageBtn;
		}

		private Context mContext;
		
		private Integer[] mThumbIds =
		{
				R.drawable.action_pic_001,R.drawable.action_pic_031,R.drawable.action_pic_071,
				R.drawable.action_pic_042,R.drawable.action_pic_058,R.drawable.action_pic_005,
				R.drawable.action_pic_010,R.drawable.action_pic_015,R.drawable.action_pic_020,
				R.drawable.action_pic_025,R.drawable.action_pic_030,R.drawable.action_pic_035,
				R.drawable.action_pic_040,R.drawable.action_pic_045,R.drawable.action_pic_050,
				R.drawable.action_pic_055,R.drawable.action_pic_060,R.drawable.action_pic_065,
				R.drawable.action_pic_070,R.drawable.action_pic_075,R.drawable.action_pic_080,
				R.drawable.action_pic_085,R.drawable.action_pic_090,R.drawable.action_pic_095,
				R.drawable.action_pic_100
		};

		private Integer[] mThumbIds_pro =
		{ 
				R.drawable.action_pic_001, R.drawable.action_pic_002, R.drawable.action_pic_003, 
				R.drawable.action_pic_004, R.drawable.action_pic_005, R.drawable.action_pic_006, 
				R.drawable.action_pic_007, R.drawable.action_pic_008, R.drawable.action_pic_009, 
				R.drawable.action_pic_010, R.drawable.action_pic_011, R.drawable.action_pic_012,
				R.drawable.action_pic_013, R.drawable.action_pic_014, R.drawable.action_pic_015,
				R.drawable.action_pic_016, R.drawable.action_pic_017, R.drawable.action_pic_018,
				R.drawable.action_pic_019, R.drawable.action_pic_020, R.drawable.action_pic_021,
				R.drawable.action_pic_022, R.drawable.action_pic_023, R.drawable.action_pic_024,
				R.drawable.action_pic_025, R.drawable.action_pic_026, R.drawable.action_pic_027,
				R.drawable.action_pic_028, R.drawable.action_pic_029, R.drawable.action_pic_030,
				R.drawable.action_pic_031, R.drawable.action_pic_032, R.drawable.action_pic_033,
				R.drawable.action_pic_034, R.drawable.action_pic_035, R.drawable.action_pic_036,
				R.drawable.action_pic_037, R.drawable.action_pic_038, R.drawable.action_pic_039,
				R.drawable.action_pic_040, R.drawable.action_pic_041, R.drawable.action_pic_042,
				R.drawable.action_pic_043, R.drawable.action_pic_044, R.drawable.action_pic_045,
				R.drawable.action_pic_046, R.drawable.action_pic_047, R.drawable.action_pic_048,
				R.drawable.action_pic_049, R.drawable.action_pic_050, R.drawable.action_pic_051,
				R.drawable.action_pic_052, R.drawable.action_pic_053, R.drawable.action_pic_054,
				R.drawable.action_pic_055, R.drawable.action_pic_056, R.drawable.action_pic_057,
				R.drawable.action_pic_058, R.drawable.action_pic_059, R.drawable.action_pic_060,
				R.drawable.action_pic_061, R.drawable.action_pic_062, R.drawable.action_pic_063,
				R.drawable.action_pic_064, R.drawable.action_pic_065, R.drawable.action_pic_066,
				R.drawable.action_pic_067, R.drawable.action_pic_068, R.drawable.action_pic_069,
				R.drawable.action_pic_070, R.drawable.action_pic_071, R.drawable.action_pic_072, 
				R.drawable.action_pic_073, R.drawable.action_pic_074, R.drawable.action_pic_075, 
				R.drawable.action_pic_076, R.drawable.action_pic_077, R.drawable.action_pic_078, 
				R.drawable.action_pic_079, R.drawable.action_pic_080, R.drawable.action_pic_081,
				R.drawable.action_pic_082, R.drawable.action_pic_083, R.drawable.action_pic_084, 
				R.drawable.action_pic_085, R.drawable.action_pic_086, R.drawable.action_pic_087, 
				R.drawable.action_pic_088, R.drawable.action_pic_089, R.drawable.action_pic_090, 
				R.drawable.action_pic_091, R.drawable.action_pic_092, R.drawable.action_pic_093, 
				R.drawable.action_pic_094, R.drawable.action_pic_095, R.drawable.action_pic_096, 
				R.drawable.action_pic_097, R.drawable.action_pic_098, R.drawable.action_pic_099,
				R.drawable.action_pic_100
		};
	}

}